//
//  ImageMsgCell.m
//  ChangChat
//
//  Created by Cocoa on 2019/8/22.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "ImageMsgCell.h"
#import <SDImageCache.h>
#import <SDWebImageDownloader.h>

@interface ImageMsgCell ()

@property (nonatomic, strong) SDWebImageDownloadToken *sdToken;
@end

@implementation ImageMsgCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    self.photoImgView = [UIImageView new];
    self.photoImgView.contentMode = UIViewContentModeScaleAspectFill;
    
    [self.msgContentView addSubview:self.photoImgView];
    [self.photoImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.msgContentView);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(50);
    }];
    UIImage *img = UIImage.k_imgName(@"msg_placeholder_img");
    self.photoImgView.image = img;
    [self photo_UpdatePhotoImgLayout];
}

- (void)setMsgModel:(MsgModel *)msgModel {
    [super setMsgModel:msgModel];
    self.msg = msgModel.msg;
}
- (void)prepareForReuse {
    [super prepareForReuse];
    if (self.sdToken) {
        [self.sdToken cancel];
    }
}

- (void)setMsg:(MsgItem *)msg {
    _msg = msg;
    UIImage *img = nil;
    if (msg.content.isHaveStr == YES) {
        UIImage *photo = [[SDImageCache sharedImageCache] imageFromCacheForKey:msg.content];
        img = photo;
    } else if (msg.filePath.isHaveStr == YES) {
        img = [[SDImageCache sharedImageCache] imageFromCacheForKey:msg.filePath];
    }
    if (img == nil) {
        self.sdToken = [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:msg.content.toURL completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
           
            if (image) {
                self.photoImgView.image = image;
//                [self photo_UpdatePhotoImgLayout];
//                [self layoutIfNeeded];
//                if (self.imgLoaded) {
//                    self.imgLoaded(self);
//                }
            }
        }];
        
    }
    if (img == nil) {
        NSData *data = [[NSData alloc] initWithBase64EncodedString:msg.s_image options:NSDataBase64DecodingIgnoreUnknownCharacters];
        img = [UIImage imageWithData:data];
    }
    if (img == nil) {
        img = UIImage.k_imgName(@"msg_placeholder_img");
    }
    self.photoImgView.image = img;
    [self photo_UpdatePhotoImgLayout];
}
- (void)photo_UpdatePhotoImgLayout {
    UIImage *img = self.photoImgView.image;
    
    CGFloat maxSize = ScreenWidth/3;
    CGFloat width = img.size.width;
    CGFloat height = img.size.height;
    if (width > height) {
        height = maxSize/width * height;
        width = maxSize;
        if (height < maxSize/3) {
            height = maxSize/3;
        }
    } else {
        width = maxSize/height * width;
        height = maxSize;
        if (width < maxSize/3) {
            width = maxSize/3;
        }
    }
    [self.photoImgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(height);
    }];
}

@end
